package com.song.EMS.dao.impl;


import com.song.EMS.bean.Administrator;
import com.song.EMS.dao.AdministratorDao;
import com.song.EMS.utils.DruidUtil;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;

public class AdministratorDaoImpl extends DruidUtil implements AdministratorDao {
    /**
     * 登录的sql语句
     */
    private static final String LOGIN_SQL = "SELECT * FROM administrator WHERE administratorName = ? AND password = ?";
    /**
     * 登录后更新登录时间与登录IP地址sql
     */
    private static final String UPDATELOGINTIME_SQL = "UPDATE administrator SET loginTime = ?,ipAddress = ? WHERE administratorName = ?";
    /**
     * 注册的sql
     */
    private static final String INSERT_SQL = "INSERT INTO administrator VALUES(NULL,?,?,?,null,NULL,?)";
    /**
     * 通过手机号或管理员名查询的sql
     */
    private static final String SELECTBYPHONE_SQL = "SELECT * FROM administrator WHERE";


    /**
     * 管理员登录
     *
     * @param administratorName 管理员名
     * @param password          管理员密码
     * @return true 登录成功
     */
    @Override
    public Administrator login(String administratorName, String password) {
        Administrator administrator = null;
        List list = new ArrayList<>();
        list.add(administratorName);
        list.add(password);

        try {
            resultSet = select(LOGIN_SQL, list);
            while(resultSet.next()) {
                administrator = new Administrator();
                administrator.setAdministratorPhone(resultSet.getString("administratorPhone"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }

        return administrator;
    }

    /**
     * 更新登录时间与登录IP地址
     *
     * @param administratorName 管理员名
     * @param ipAddress         登录IP地址
     * @param loginTime         登录时间
     */
    @Override
    public void updateLoginTime(String administratorName, String ipAddress, Timestamp loginTime) {
        List list = new ArrayList<>();
        list.add(loginTime);
        list.add(ipAddress);
        list.add(administratorName);

        try {
            update(UPDATELOGINTIME_SQL, list);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
    }

    /**
     * 管理员的注册
     *
     * @param administrator 管理员注册信息
     * @return 1成功 2管理员名已被注册  3手机号已被注册 4失败
     */
    @Override
    public Integer register(Administrator administrator) {

        List list1 = new ArrayList<>();
        list1.add(administrator.getAdministratorName());

        try {

            List list = new ArrayList<>();
            list.add(administrator.getAdministratorName());
            list.add(administrator.getAdministratorPhone());
            list.add(administrator.getPassword());
            list.add(administrator.getRegisterTime());

            if (update(INSERT_SQL, list) > 0) {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }

        return -2;

    }

    /**
     * 通过手机号或管理员名查询管理员
     *
     * @param administratorPhone 管理员的手机号
     * @return 查询的管理员对象
     */
    @Override
    public Administrator selectByPhone(String administratorPhone, String administratorName) {
        Administrator administrator = new Administrator();
        String sql = null;
        try {
            List list = new ArrayList<>();

            if (administratorName != null && !"".equals(administratorName)) {
                sql = SELECTBYPHONE_SQL + " administratorName = ?";
                list.add(administratorName);
            } else if (administratorPhone != null && !"".equals(administratorPhone)) {
                sql = SELECTBYPHONE_SQL + " administratorPhone = ?";
                list.add(administratorPhone);
            }
            resultSet = select(sql, list);
            while (resultSet.next()) {
                administrator.setAdministratorName(resultSet.getString("administratorName"));
                administrator.setPassword(resultSet.getString("password"));
                administrator.setAdministratorPhone(resultSet.getString("administratorPhone"));
                administrator.setAdministratorId(resultSet.getInt("administratorId"));
                return administrator;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return null;

    }
}
